package com.cy.pj.sys.service.impl;

import com.cy.pj.common.pojo.CheckBox;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色业务对象
 */
@Transactional(rollbackFor = Throwable.class,
timeout = 10,
isolation = Isolation.READ_COMMITTED)
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Override
    public List<SysRole> findRoles(SysRole sysRole) {
        return sysRoleDao.selectRoles(sysRole);
    }

    @Override
    public SysRole findById(Integer id) {
        return sysRoleDao.selectById(id);
    }

    @Override
    public int saveRole(SysRole sysRole) {
        int rows=sysRoleDao.insertRole(sysRole);
        sysRoleMenuDao.insertRoleMenus(sysRole.getId(), sysRole.getMenuIds());
        return rows;
    }

    @Override
    public int updateRole(SysRole sysRole) {
        //跟新角色自身信息
        int rows=sysRoleDao.updateRole(sysRole);
        //更新角色菜单关系数据---删除 添加
        sysRoleMenuDao.deleteByRoleId(sysRole.getId());
        sysRoleMenuDao.insertRoleMenus(sysRole.getId(), sysRole.getMenuIds());
        return rows;
    }

    @Override
    public List<CheckBox> findCheckRoles() {
        return sysRoleDao.selectCheckRoles();
    }
}
